package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.entity.Slide;
import cn.kgc.ssm.mapper.SlideMapper;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.SlideService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/11/20
 * @description
 */
@Service
public class SlideServiceImpl implements SlideService {


    @Autowired
    private SlideMapper slideMapper;

    @Override
    public ResponseResult slidePage(Integer page, Integer limit) {

        PageHelper.startPage(page,limit);
       List<Slide> tableData =  slideMapper.selectSlide();
        PageInfo<Slide> slidePageInfo = new PageInfo<>(tableData);
        long total = slidePageInfo.getTotal();

        return ResponseResult.success().data(tableData).count(total);
    }

    @Override
    public ResponseResult addUploadInfo(Slide slide) {

        slideMapper.insertUploadInfo(slide);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult topPicture(Integer id) {
        slideMapper.updatePicture(id);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult deletePicture(String uuid) {

        //  1. 删除本地图片资源
        File f = new File("D:/upload/slides/" + uuid);
        if (f.exists()){
            f.delete();
        }
        //  2. 删除图片资源数据库记录
        slideMapper.deletePicture(uuid);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult pictureList(Integer limit) {

          List<Slide> slides =  slideMapper.selectPictureList(limit);
        return ResponseResult.success().data(slides);
    }
}
